//
//  CategorySelectListView.swift
//  KoiAccounting
//
//  类别选择列表
//
//  Created by 纪洪文 on 02.04.25.
//

import SwiftUI

struct CategorySelectListView: View {
    
    @Environment(\.presentationMode) var presentationMode
    
    @StateObject var viewModel = CategorySelectListViewModel()
    
    var title: String
    
    var selectedCategory: AccountingCategory
    
    var categoryItemClick: (AccountingCategory) -> Void
    
    var body: some View {
        VStack {
            ZStack {
                HStack {
                    Spacer()
                    Text(title)
                        .font(.system(size: 16))
                        .bold()
                        .foregroundColor(Color.koiColorTitle)
                    Spacer()
                }
                HStack {
                    Spacer()
                    Button(action: {
                        presentationMode.wrappedValue.dismiss()
                    }) {
                        Image("koi_ic_close")
                            .padding(.horizontal, 20)
                    }
                }
            }
            .padding(EdgeInsets(top: 11, leading: 0, bottom: 11, trailing: 0))
            List {
                ForEach(viewModel.categoryList.indices, id:\.self) { index in
                    let category = viewModel.categoryList[index]
                    Button(action: {
                        categoryItemClick(category)
                        presentationMode.wrappedValue.dismiss()
                    }) {
                        CategoryManagerItemView(item: category, moreIconClick: {clickItem in })
                            .padding(EdgeInsets(top: 10, leading: 14, bottom: 10, trailing: 10))
                            .background(Color.koiColorBackgroundItem)
                            .cornerRadius(16)
                            .padding(.vertical, 5)
                    }
                    .listRowSeparator(.hidden)
                    .listRowInsets(EdgeInsets())
                    .listRowBackground(Color.clear)
                }
            }
            .listStyle(.plain)
            .padding(EdgeInsets(top: 0, leading: 20, bottom: 0, trailing:20)) // 去除边距
            .ignoresSafeArea(.container, edges: .bottom)
        }
        .onAppear {
            viewModel.loadCategorys(selectedCategory: selectedCategory)
        }
    }
}

#Preview {
    CategorySelectListView(title: "类别选择", selectedCategory: AccountingCategory(name: "餐饮")) { _ in }
}
